class Node {
    constructor(data) {
        this.element = data;
        this.next = undefined;
    }
}
class LinkedList {
    constructor() {
        this.head = undefined;
    }

    //将数组转换为链表
    append(arr) {

        let node = new Node(arr[0]);
        this.head = node;
        for (let i = 1; i < arr.length; i++) {
            //数组循环存储数据
            node.next = new Node(arr[i]);
            //链表的节点循环
            node = node.next;
        }

    }


    //实现逆置
    reserve(head) {
        //基线条件
        if (head.next == null) {
            //返回head
            return head;
        }
        //调用自身
        let restNode = this.reserve(head.next);
        let current = head.next;
        current.next = head;
        head.next = undefined;

        this.head = restNode;
        return this.head;

    }

    toString() {
        //按照情况去进行讨论处理

        if (this.head == null) {
            return '';
        }
        let str = this.head.element;
        let current = this.head.next;
        while (current != null) {
            //隐式转换
            //数值+" "
            str = str + " " + current.element;
            current = current.next;
        }
        return str;
    }


}
const arr = [1, 2, 3, 99];
const list = new LinkedList();
list.append(arr);
list.reserve(list.head);
console.log(list.toString());